package com.homework.test03;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author south wind
 * @version 1.0
 * @date 2025/4/7 19:23
 * @description  学生实体层
 */
public class StudentImpl implements StuDao{
    public static final Scanner INPUT = new Scanner(System.in);
    public static final ArrayList<Student> studentList= new ArrayList<Student>();
    public static final String FILE_PATH = "D:\\JavaSE\\basic_code\\day24_IO_code\\src\\com\\homework\\test03\\student.txt";
    //读取相应文件的信息
    static {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(FILE_PATH));
            //1.定义字符串接受
            String line = null;
            //2.循环读取
            while ((line = br.readLine()) != null) {
                //3.改装字符串
                String[] str = line.split("-");
                //4.将字符串改装成封装对象
                Student student = new Student(
                        str[0],
                        Integer.parseInt(str[1]),
                        str[2],
                        Integer.parseInt(str[3]));

                studentList.add(student);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭流
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
    * 添加学生信息*/
    @Override
    public void insertStu() {
        System.out.println("请输入学生姓名：");
        String name = INPUT.nextLine();
        System.out.println("请输入学生年龄：");
        int age = INPUT.nextInt();
        System.out.println("请输入学生性别：");
        String sex = INPUT.next();
        System.out.println("请输入学生成绩：");
        int score = INPUT.nextInt();
        //封装对象
        Student student = new Student(name,age,sex,score);

        //向上添加信息
        studentList.add(student);
        //将集合信息同步到文件中
        appendWriteStudent(student);
        System.out.println("添加成功！");

    }
    /*
        删除学生信息
    */
    @Override
    public void deleteStu() {
        System.out.println("请输入要删除学生的姓名：");
        String name = INPUT.nextLine();
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)){
                studentList.remove(i);
                System.out.println("删除成功！");
                return;
            }
        }
    }
    /*
    *  修改学生信息
    * */
    @Override
    public void updateStu() {
        System.out.println("请输入要修改学生的姓名：");
        String name = INPUT.nextLine();
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().equals(name)){
                System.out.println("请输入要修改学生的姓名：");
                String newName = INPUT.nextLine();
                System.out.println("请输入要修改学生的年龄：");
                int newAge = INPUT.nextInt();
                System.out.println("请输入要修改学生的性别：");
                String newSex = INPUT.next();
                System.out.println("请输入要修改学生的成绩：");
                int newScore = INPUT.nextInt();

                studentList.get(i).setName(newName);
                studentList.get(i).setAge(newAge);
                studentList.get(i).setSex(newSex);
                studentList.get(i).setScore(newScore);

                System.out.println("修改成功！");
            }
        }
    }
    //将集合信息同步到文件中
    public void appendWriteStudent(Student student){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(FILE_PATH,true));
            //将改装信息编程字符串
            StringBuilder sb = new StringBuilder();
            sb.append(student.getName());
            sb.append("-");
            sb.append(student.getAge());
            sb.append("-");
            sb.append(student.getSex());
            sb.append("-");
            sb.append(student.getScore());

            //将sb转成String字符串
            String lineItem = sb.toString();
            //写到文件中
            //需要先换行
            bw.newLine();
            bw.write(lineItem);
            //写完刷新
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
